#include "view_calories.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "language_text.h"
int32_t view_calories_display(int32_t view)
{
    // Expects(view >= view_calories);
    // Expects(view < view_calories + VIEW_LEVEL_0_SUB_COUNT);
    if (view != view_calories)
    {
        return 0;
    }
    if (disp_ctrl.loop_cnt == 0)
    {
#if 0
#warning debug
        disp_ctrl.auto_refresh_time = 1000;
        // disp_ctrl.com_data.i_temp[0] = disp_ctrl.auto_refresh_count % 11;
        disp_ctrl.com_data.i_temp[0] = 10;
        disp_ctrl.com_data.i_temp[1] = 10;
#else
        disp_ctrl.com_data.i_temp[0] = sport_get_calorie();
        disp_ctrl.com_data.i_temp[1] = system_config_get_target_calorie();
#endif
    }
    int x = 0;
    int y = 0;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_B216_caroies_2);

    x = 77;
    y = 37;
#ifdef CUSTOM_UI
    int const temp_index_start = 50;
    int custom_ui_index = 0;

    for (int i = 0; i < CALORIES_BACK_COUNT; i++)
    {
        disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_calories_back_start + i, temp_index_start + custom_ui_index, -1, -1);
        ++custom_ui_index;
    }
#if CALORIES_GIF_COUNT > 0
    disp_show_resource_with_x_y(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_resource_key_calories_gif_start + disp_ctrl.auto_refresh_count % HEART_RATE_GIF_COUNT,
                                temp_index_start + custom_ui_index, -1, -1);
    ++custom_ui_index;
#endif
    // bool ret = disp_show_localize(DISP_LFS_FILE_INDEX_0 + custom_ui_index, localize_string_key_calories_text, temp_index_start + custom_ui_index);
    // if (!ret)
    // {
    //     _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_B217_cals);
    // }
    // ++custom_ui_index;

    x = 120;
    y = 157;
    disp_draw_user_image_num_mid_ignore(x, y, RGB565_COLOR_BLACK, disp_ctrl.com_data.i_temp[0], 21, num_N400_dig_19x42_TAB);
#else
    if (disp_ctrl.com_data.uint8_temp[100])
    {
        y -= 3;
        disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[101]), 0);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_B217_cals);
    }

    x = 43;
    y = 72;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_B222_horizontalline);

    x = 106;
    y = 91;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_B216_caroies_icon);

    x = 120;
    y = 157;
    disp_draw_user_image_num_mid_ignore(x, y, RGB565_COLOR_BLACK, disp_ctrl.com_data.i_temp[0], 21, num_N400_dig_19x42_TAB);
    user_cycle_percent_param_t const param = {
        .base = step_calorie_ring_base,
        .transparent_color = RGB565_COLOR_BLACK,
        .full_color = 0xeca6,
        .empty_color = 0x4141,
        .x = 0,
        .y = 0,
        .target_num = disp_ctrl.com_data.i_temp[1],
        .presently_num = disp_ctrl.com_data.i_temp[0],
        .temp_0 = &disp_ctrl.com_data.i_temp[2],
        .temp_1 = &disp_ctrl.com_data.i_temp[3],
        .temp_2 = &disp_ctrl.com_data.f_temp[4],
        .temp_3 = &disp_ctrl.com_data.i_temp[5],
        .temp_4 = &disp_ctrl.com_data.i_temp[6],
        .line_width = 7,
    };
    disp_draw_user_image_cycle_percent(&param);
    x = 213;
    y = 91;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I201_btn_up);
    y = 137;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_I202_btn_down);

#endif
    return 0;
}
view_event_result_t view_calories_event(int32_t view, event_param_t event)
{
    Expects(view >= view_calories);
    Expects(view < view_calories + VIEW_LEVEL_0_SUB_COUNT);

    switch (event.event)
    {
    case event_move_level_down:
        return view_event_result_handled;
    case event_view_entry:
    {
        uint8_t *buff = &disp_ctrl.com_data.uint8_temp[101];
        uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
        disp_ctrl.com_data.uint8_temp[100] = get_language_text(language_text_index_Calories, buff, buff_len);
        return view_event_result_handled;
    }
    }

    return view_event_result_not_handled;
}
